function formAddNewEventValidate() {
    // validator for formAddNewEvent
    $.ketchup
    .createErrorContainer(function(form, el) {
        return $('<ul/>', {
        'class': 'alert-message error-for-ketchup'
        }).insertAfter(el);
    })
    
    .addErrorMessages(function(form, el, container, messages) {
        container.html('');
        for(var i = 0; i < messages.length; i++) {
        $('<li/>', {
        text: messages[i]
        }).appendTo(container);
        }
    })
    
    .showErrorContainer(function(form, el, container) {
        container.slideDown('fast');
    })
    
    .hideErrorContainer(function(form, el, container) {
        container.slideUp('fast');
    });

    $('#formAddNewEvent')
        .bind('formIsValid', function(event, form) {
            formAddNewEventSubmit();
        })
        .ketchup({
            validateEvents: 'blur focus keyup change'
        });
    $('#formAddNewEvent').submit(function(){return false;});
}

function formAddNewEventDateRangePicker() {
    var dates = 
    $( "#timeStart, #timeEnd" ).datepicker({
        defaultDate: "-1w",
        maxDate: '+0d',
        changeYear: true,
        onSelect: function( selectedDate ) {
            var option = this.id == "timeStart" ? "minDate" : "maxDate",
            instance = $( this ).data( "datepicker" ),
            date = $.datepicker.parseDate(
                instance.settings.dateFormat ||
                $.datepicker._defaults.dateFormat,
                selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
            
            this.focus();
            this.blur();
        }
    });
}

function formAddNewEventSubmit() {
    $("#formAddNewEventErrors").html("");
    
    $.post(
        $("#controller").val() + "/ajax-post-add-new-event",
        {
            name:       $('#formAddNewEvent #name').val(),
            type:       $('#formAddNewEvent #type').val(),
            timeStart:  $('#formAddNewEvent #timeStart').val(),
            timeEnd:    $('#formAddNewEvent #timeEnd').val()
        },
        function(res) {
            var result = $.parseJSON(res);
            if (result.status == "ERROR") {
                for (var errorMsg in result.msgs.bsns) {
                    $("#formAddNewEventErrors").append(
                        "<div class=\"alert-message error\">"+result.msgs.bsns[errorMsg]+"</div>");
                }
            }
            else if (result.status == "OK") {
                var timeStart = $('#formAddNewEvent #timeStart').datepicker("getDate");
                var redirectUrl = $("#serverSpec").val() + "/" + $("#controller").val() +
                                    "?timeStart=" + timeStart.getFullYear() + "-" +
                                    (timeStart.getMonth()) + "&timeEnd=" +
                                    timeStart.getFullYear() + "-" + (timeStart.getMonth() + 1);
                $('#modalAddEvent').modal('hide');
                
                window.location = redirectUrl;
            }
        })
        .error(function() {alert('ERROR');});
}